package com.qc31.monitorlib.ui.alarms

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.qc31.baselib.base.BaseBarActivity
import com.qc31.baselib.custom.MToolbar
import com.qc31.baselib.rx.LiveDataBus
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.baselib.utils.isFastClick
import com.qc31.baselib.utils.setResultTo
import com.qc31.comlib.Keys
import com.qc31.comlib.alarm.AlarmDataUtil
import com.qc31.comlib.alarm.ReportSetModel
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.ActivitySubAlarmBinding
import com.qc31.monitorlib.ui.adapter.ChooseAlarmAdapter

/** @Author Jasper @Time 2021/11/22 9:51 @Desc */
class AlarmChooseActivity :
    BaseBarActivity<ActivitySubAlarmBinding>(ActivitySubAlarmBinding::inflate) {
    private val mAdapter by lazy { ChooseAlarmAdapter() }

    //是否是安全报警
    private var isAdas = false
    private var ids = ""

    private var isCarChoose = false
    override fun initView() {
        isAdas = intent.getBooleanExtra(Keys.INTENT_OTHER, false)
        isCarChoose = intent.getBooleanExtra("isCarChoose", false)
        ids = intent.getStringExtra(Keys.INTENT_DATA) ?: ""
        ToolbarHelper.setToolbar(
            this,
            R.string.desc_alarm_type,
            R.string.desc_com_sure,
            object : MToolbar.IToolbarCallback {
                override fun actionOnClick(action: TextView) {
                    if (isFastClick()) return
                    updateAlarms()
                }
            }
        )
        binding.recyclerAlarm.layoutManager = LinearLayoutManager(this)
        binding.recyclerAlarm.adapter = mAdapter
        initAlarmData()

    }

    override fun setListener() {
        binding.tvAlarmAllCheck.setOnClickListener {
            binding.tvAlarmAllCheck.isSelected = true
            binding.tvAlarmAllUnCheck.isSelected = false
            checkAll()
        }

        binding.tvAlarmAllUnCheck.setOnClickListener {
            binding.tvAlarmAllCheck.isSelected = false
            binding.tvAlarmAllUnCheck.isSelected = true
            uncheckAll()
        }

        mAdapter.setAllBack(object : ChooseAlarmAdapter.ChooseAllBack {
            override fun onChooseAll() {
                setAllChoose()
            }
        })
    }

    /** 1全选 0部分选择 -1全不选 */
    private fun isAllBack(): Int {
        var allChoose = 0
        var allNotChoose = 0
        mAdapter.data.forEach {
            if (it.isAllChoose) {
                allNotChoose++
            } else {
                allChoose++
                it.children.forEach { entity ->
                    if (entity.isChoose) allNotChoose++
                }
            }
        }
        if (allChoose == 0) return 1
        if (allNotChoose == 0) return -1
        return 0
    }

    private fun initAlarmData() {
        mAdapter.setList(
            AlarmDataUtil.instance.getAlarmByIds(isAdas, ids)
        )
        setAllChoose()
    }

    // 全选
    private fun checkAll() {
        mAdapter.data.forEach {
            it.isAllChoose = true
            it.children.forEach { children ->
                children.isChoose = true
            }
        }
        mAdapter.notifyDataSetChanged()
    }

    //全不选
    private fun uncheckAll() {
        mAdapter.data.forEach {
            it.isAllChoose = false
            it.children.forEach { children ->
                children.isChoose = false
            }
        }
        mAdapter.notifyDataSetChanged()
    }


    private fun setAllChoose() {
        when (isAllBack()) {
            0 -> {
                binding.tvAlarmAllCheck.isSelected = false
                binding.tvAlarmAllUnCheck.isSelected = false
            }
            1 -> {
                binding.tvAlarmAllCheck.isSelected = true
                binding.tvAlarmAllUnCheck.isSelected = false
            }
            else -> {
                binding.tvAlarmAllCheck.isSelected = false
                binding.tvAlarmAllUnCheck.isSelected = true
            }
        }
    }

    private fun updateAlarms() {
        val alarmIds = AlarmDataUtil.instance.getAlarmIds(mAdapter.data, isAdas)
        setResultTo(this) {
            putExtra("alarmIds", alarmIds)
        }
        if (isCarChoose) {
            if (isAdas) {
                ReportSetModel.instance.setAdasAlarmIds(alarmIds)
            } else {
                ReportSetModel.instance.setNormalAlarmIds(alarmIds)
            }
            LiveDataBus.get().with("AlarmChoose").postValue(alarmIds)
        }
        finish()
    }
}


